package com.fractalist.base.core.utils;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;

public class FileUtil {
	private static String ROOT_PATH = null;

	public static int getValidateImageType(FormFile file, String type) throws Exception {

		String name = file.getFileName().toLowerCase();

		if ("10".equals(type) || "11".equals(type)) {
			if (!name.matches(".*\\.(jpg|png)")) {
				return 0;
			}
		} else {
			if (!name.matches(".*\\.(jpg|png|gif)")) {
				return 0;
			}
		}

//		if (file.getFileSize() > 1024 * 1024) {
//			return 99;
//		} else {
			BufferedImage bi = ImageIO.read(file.getInputStream());
			if (null == bi) {
				return 0;
			}
			int height = bi.getHeight();
			int width = bi.getWidth();
			if (type.equals("2")) {
				if (width == 70 && height == 70){
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 6;
					}
				}else{
					return 0;
				}
			} else if (type.equals("3")) {
				if (width == 150 && height == 30){
					if (file.getFileSize() > 10 * 1024) {
						return 99;
					}else{
						return 3;
					}
				}else if (width == 640 && height == 96){
					if (file.getFileSize() > 25 * 1024) {
						return 99;
					}else{
						return 4;
					}
				}else if (width == 768 && height == 110){
					if (file.getFileSize() > 30 * 1024) {
						return 99;
					}else{
						return 5;
					}
				}else if (width == 240 && height == 60){
					if (file.getFileSize() > 15 * 1024) {
						return 99;
					}else{
						return 11;
					}
				}else{
					if (file.getFileSize() > 1024 * 1024) {
						return 99;
					}else{
						return 12;
					}
				}
			} else if (type.equals("4")) {
				if (width == 640 && height == 960){
					if (file.getFileSize() > 130 * 1024) {
						return 99;
					}else{
						return 7;
					}
				}else{
					if (file.getFileSize() > 130 * 1024) {
						return 99;
					}else{
						return 8;
					}
				}
			} else if (type.equals("6")) {
				if (width == 70 && height == 70){
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 6;
					}
				}else if (width == 640 && height == 96){
					if (file.getFileSize() > 25 * 1024) {
						return 99;
					}else{
						return 4;
					}
				}else if (width == 768 && height == 110){
					if (file.getFileSize() > 30 * 1024) {
						return 99;
					}else{
						return 5;
					}
				}else{
					return 0;
				}
			} else if (type.equals("8")) {
				if (width == 640 && height == 96)
					if (file.getFileSize() > 25 * 1024) {
						return 99;
					}else{
						return 4;
					}
				else if (width == 768 && height == 110)
					if (file.getFileSize() > 30 * 1024) {
						return 99;
					}else{
						return 5;
					}
				else
					return 0;
			} else if (type.equals("10")) {
				if (width == 640 && height == 96)
					if (file.getFileSize() > 25 * 1024) {
						return 99;
					}else{
						return 4;
					}
				else if (width == 72 && height == 72)
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 9;
					}
				else if (width == 48 && height == 48)
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 10;
					}
				else if (width == 320 && height == 480)
					if (file.getFileSize() > 70 * 1024) {
						return 99;
					}else{
						return 20;
					}
				else{
					return 0;
				}
			} else if (type.equals("11")) {
				if (width == 72 && height == 72){
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 9;
					}
				}else{
					return 0;
				}
			} else if (type.equals("13")) {
				if (width == 72 && height == 72){
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 9;
					}
				}else{
					return 0;
				}
			} else {
				if (width == 320 && height == 48){
					if (file.getFileSize() > 15 * 1024) {
						return 99;
					}else{
						return 1;
					}
				}else if (width == 480 && height == 64){
					if (file.getFileSize() > 15 * 1024) {
						return 99;
					}else{
						return 2;
					}
				}else if (width == 150 && height == 30){
					if (file.getFileSize() > 10 * 1024) {
						return 99;
					}else{
						return 3;
					}
				}else if (width == 640 && height == 96){
					if (file.getFileSize() > 25 * 1024) {
						return 99;
					}else{
						return 4;
					}
				}else if (width == 768 && height == 110){
					if (file.getFileSize() > 30 * 1024) {
						return 99;
					}else{
						return 5;
					}
				}else if (width == 70 && height == 70){
					if (file.getFileSize() > 5 * 1024) {
						return 99;
					}else{
						return 6;
					}
				}else if (width == 640 && height == 960){
					if (file.getFileSize() > 130 * 1024) {
						return 99;
					}else{
						return 7;
					}
				}else{
					return 0;
				}
			}
//		}
		// 系统提供的icon图标占用6;图片大于1MB占用99
	}

	public static String getHeightAndWidthByPic(FormFile file) throws Exception {
		BufferedImage bi = ImageIO.read(file.getInputStream());
		if (null == bi) {
			return "";
		}
		int height = bi.getHeight();
		int width = bi.getWidth();
		return height+","+width;
	}

	/**
	 * 验证劳务协议图片、附件的上传文件
	 * @param file 上传文件
	 * @param type 1=身份证图片 ; 2=银行卡图片 ; 其他数字=劳务协议附件
	 * @return
	 * @throws Exception
	 */
	public static int getUserFileType(FormFile file, String type) throws Exception {
		String name = file.getFileName().toLowerCase();

		//验证身份证图片'
		if("0".equals(type)){
			if (!name.matches(".*\\.(sis|sisx|ipa|jar|exe|rar|cab|cod|jad|apk)")) {
				return 0;
			}
			//验证文件大小
			if (file.getFileSize() > 100 * 1024 * 1024) {
				return 99;
			}
			return 1;
		}else if ("1".equals(type)) {
			//验证文件格式
			if (!name.matches(".*\\.(jpg|jpeg|bmp)")) {
				return 0;
			}
			//验证文件大小
			if (file.getFileSize() > 1024 * 1024) {
				return 99;
			}
			return 1;
//			else {
//				BufferedImage bi = ImageIO.read(file.getInputStream());
//				if (null == bi) {
//					return 0;
//				}
//				int height = bi.getHeight();
//				int width = bi.getWidth();
//				if (width == 200 && height == 280) {
//					return 1;
//				} else {
//					return 0;
//				}
//			}
		}
		//验证银行卡图片
		else if ("2".equals(type)) {
			//验证文件格式
			if (!name.matches(".*\\.(jpg|jpeg|bmp)")) {
				return 0;
			}
			//验证文件大小
			if (file.getFileSize() > 1024 * 1024) {
				return 99;
			}
			return 1;
//			else {
//				BufferedImage bi = ImageIO.read(file.getInputStream());
//				if (null == bi) {
//					return 0;
//				}
//				int height = bi.getHeight();
//				int width = bi.getWidth();
//				if (width == 200 && height == 140) {
//					return 1;
//				} else {
//					return 0;
//				}
//			}
		}
		//验证协议附件
		else {
			//验证文件格式
			if (!name.matches(".*\\.(rar|pdf)")) {
				return 0;
			}
			//验证文件大小
			if (file.getFileSize() > 2 * 1024 * 1024) {
				return 99;
			} else {
				return 1;
			}
		}
	}

	public static boolean saveFile(FormFile file, String path) throws Exception {
		OutputStream out = null;
		InputStream in = null;
		try {
			out = new FileOutputStream(new File(path));
			in = file.getInputStream();
			int i = 0;
			while ((i = in.read()) != -1) {
				out.write(i);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			out.close();
			in.close();
		}
		return true;
	}

	public static boolean dropFile(String fileName) throws Exception {
		File file = new File(fileName);
		return file.delete();
	}

	public static void main(String[] args) {
		System.out.println(getClassPath());
	}

	/**
	 * 得到当前ClassLoader的在操作系统中的绝对路径
	 *
	 * @return
	 */
	public static String getClassPath() {
		return FileUtil.class.getClassLoader().getResource("").getPath().substring(0);
	}

	/**
	 * 返回系统当前目录
	 *
	 * @return
	 */
	public static String getProjectRootPath() {

		if (ROOT_PATH != null)
			return ROOT_PATH;

		String root = getClassPath();
		root = root.replaceAll("%20", " ");
		root = root.substring(0, root.length() - "WEB-INF/classes/".length());
		ROOT_PATH = root;
		return ROOT_PATH;
	}

	public static String readFile(String path, String encode) {

		StringBuilder sb = new StringBuilder();
		BufferedReader br = null;
		String line = "";
		File file = new File(path);
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode));

			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append("\n");

			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * 获取struts上传的文件名，将文件名替换为16位随机数
	 *
	 * @param formFile
	 * @param request
	 * @return
	 */
	public static String getRamdonFileName(FormFile formFile, HttpServletRequest request) {

		if (formFile != null && !StringUtil.isnull2(formFile.getFileName())) {

			int index = formFile.getFileName().toLowerCase().lastIndexOf(".");
			if (index > -1) {
				IPTimeStamp stamp = new IPTimeStamp(request.getRemoteAddr());// 生成16位随机数字
				// 取文件名，将文件名换为生成的16位随机数字
				String fileName = stamp.getIPTimeStamp() + formFile.getFileName().toLowerCase().substring(index);
				return fileName;
			}
		}
		return "";

	}
}
